import 'package:flutter/material.dart';
import 'package:my_flutter_github/MyStyle.dart';
import 'package:my_flutter_github/common/dao/UserDao.dart';
import 'package:my_flutter_github/model/User.dart';
import 'package:my_flutter_github/redux/MyState.dart';
import 'package:my_flutter_github/utils/CommonUtils.dart';
import 'package:my_flutter_github/widgets/CardItem.dart';
import 'package:redux/redux.dart';
import 'package:flutter_redux/flutter_redux.dart';

class UserInfoPage extends StatefulWidget {
  @override
  _UserInfoPageState createState() => _UserInfoPageState();
}

class _UserInfoPageState extends State<UserInfoPage> {

  _renderItem(IconData leftIcon, String title, String value, VoidCallback onPressed) {
    return new CardItem(
      child: new RawMaterialButton(
        onPressed: onPressed,
        materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
        padding: const EdgeInsets.all(15.0),
        constraints: const BoxConstraints(minWidth: 0.0, minHeight: 0.0),
        child: new Row(
          children: <Widget>[
            new Icon(leftIcon),
            new Container(
              width: 10.0,
            ),
            new Text(title, style: MyConstants.normalSubText),
            new Container(
              width: 10.0,
            ),
            new Expanded(child: new Text(value, style: MyConstants.normalText)),
            new Container(
              width: 10.0,
            ),
            new Icon(MyIcons.REPOS_ITEM_NEXT, size: 12.0),
          ],
        ),
      ),
    );
  }

  static const String user_profile_name = "名字";
  static const String user_profile_email = "邮箱";
  static const String user_profile_link = "链接";
  static const String user_profile_org = "公司";
  static const String user_profile_location = "位置";
  static const String user_profile_info = "简介";

  _showEditDialog(String title, String value, String key, Store store) {
    String content = value ?? "";

    CommonUtils.showEditDialog(
      context: context,
      dialogTitle: title,
      onContentChanged:  (String value) {

      },
      onPressed: (){
        if (content == null || content.length == 0) {
          return;
        }
        CommonUtils.showLoadingDialog(context);
        UserDao.updateUserDao({key: content}, store).then((res) {
          Navigator.of(context).pop();
          if(res != null && res.result) {
            Navigator.of(context).pop();
          }
        });
      },
      valueController: new TextEditingController(text: value),
      needTitle: false,
    );
  }

  List<Widget> _renderList(User userInfo, Store store) {
    return [
      _renderItem(Icons.info, user_profile_name, userInfo.name ?? "---", () {
        _showEditDialog(user_profile_name, userInfo.name, "name", store);
      }),
      _renderItem(Icons.email, user_profile_email, userInfo.email ?? "---", () {
        _showEditDialog(user_profile_email, userInfo.email, "email", store);
      }),
      _renderItem(Icons.link,user_profile_link, userInfo.blog ?? "---", () {
        _showEditDialog(user_profile_link, userInfo.blog, "blog", store);
      }),
      _renderItem(Icons.group, user_profile_org, userInfo.company ?? "---", () {
        _showEditDialog(user_profile_org, userInfo.company, "company", store);
      }),
      _renderItem(Icons.location_on, user_profile_location, userInfo.location ?? "---", () {
        _showEditDialog(user_profile_location, userInfo.location, "location", store);
      }),
      _renderItem(Icons.message, user_profile_info, userInfo.bio ?? "---", () {
        _showEditDialog(user_profile_info, userInfo.bio, "bio", store);
      }),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return StoreBuilder<MyState>(builder: (context, store) {
      return Scaffold(
          appBar: AppBar(title: Text("个人信息"),),
        body: Container(
          color: Colors.white,
          child: SingleChildScrollView(
            child: Column(
              children: _renderList(store.state.userInfo, store),
            ),
          ),
        ),
      );
    });
  }
}
